package com.ivt.controller.console;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ivt.core.util.Constants;
import com.ivt.model.Notice;
import com.ivt.model.view.QueryCriteria;
import com.ivt.model.view.Rst;
import com.ivt.service.NoticeService;

@Controller
@RequestMapping("/console/notice")
public class NoticeController {

	@Resource
	private NoticeService noticeService;


	@ModelAttribute
	public void attr(ModelMap view) {
		view.addAttribute("moduleName", "notice");
		view.addAttribute("moduleTitle", "公告信息");
	}

	@RequestMapping("/create")
	public String create(ModelMap view) {
		view.addAttribute("moduleAction", "save");
		return "alone:console.notice-create";
	}

	@RequestMapping("/list")
	public String list(ModelMap view) {

		return "alone:console.notice-list";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap view) {

		Notice notice = noticeService.getNotice(id);

		view.addAttribute("row", notice);
		view.addAttribute("moduleAction", "update");
		return "alone:console.notice-modify";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Rst save(Notice notice) {

		Rst rst = new Rst();
		notice = noticeService.create(notice);
		if (0 < notice.getNoticeId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/pagination")
	@ResponseBody
	public Rst pagination(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		List<Notice> list = noticeService.queryClassesByCriteria(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(list);
		return rst;
	}

	@RequestMapping("/pages")
	@ResponseBody
	public Rst pages(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		long count = noticeService.queryCount(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(count);
		return rst;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Rst update(Notice notice) {
		Rst rst = new Rst();
		notice = noticeService.update(notice);
		if (0 < notice.getNoticeId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Rst update(Long[] ids) {
		Rst rst = new Rst();
		int c = noticeService.deletes(ids);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("操作成功");
		rst.setData(c);
		return rst;
	}
}
